'''
Created on 2020年1月30日
获取所有股票数据
@author: JM
'''
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine
import time
from app_config import get_pro, get_engine_ts

pro = get_pro()
engine_ts = get_engine_ts()


def read_data():
    sql = """SELECT * FROM dividend"""
    df = pd.read_sql_query(sql, engine_ts)
    return df


def write_data(df_):
    res = df_.to_sql('dividend', engine_ts, index=False, if_exists='append', chunksize=5000)
    print(res)


def get_data(_ts_code):
    df = pro.dividend(ts_code=_ts_code,
                      fields='ts_code,end_date,ann_date,div_proc,stk_div,stk_bo_rate,stk_co_rate,cash_div,'
                             'cash_div_tax,record_date,ex_date,pay_date,div_listdate,imp_ann_date,base_date,base_share')
    return df


def get_daily(_ts_code):
    for _ in range(10):
        try:
            df = get_data(_ts_code)
            write_data(df)
        except Exception as e:
            print(f"An unexpected error occurred: {e}")
            time.sleep(10)
        else:
            return
    print("error error error error error" + _ts_code)


if __name__ == '__main__':

    i = 0
    from _stock_basic import get_data as get_stock_basic

    stock_basic = get_stock_basic()

    for index, row in stock_basic.iterrows():  #
        print(++i)
        # 每天的交易日期
        ts_code = row["ts_code"]
        print(str(row["name"]) + str(ts_code))
        get_daily(ts_code)
